package com.example.zaq.rest;

import com.example.zaq.dao.AspUserDao;
import com.example.zaq.model.AspUser;
import com.example.zaq.model.UpdatePwd;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//import com.example.zaq.exception.AspException;

/**
 * @author 阿恩
 * @date 2021/3/1215:11
 *
 * 20210312    zaq    增加用户登录验证API
 * 20210315    zaq    注释异常部分，与前段进行连接测试
 * 20210318    zaq    修改用户登录验证入参并进行测试
 * 20210517    zaq    新增用户注册API
 * 20210520    zaq    修改注释，新增地址
 * 20210605    zaq    新增管理员查询所有用户信息
 */
@RestController
@RequestMapping("/user")
public class AspUserRest {
    @Autowired
    SqlSessionFactory factory;

    /**
     * 用户销户
     * http://localhost:9090/user/delete
     * @param body
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public AspUser delete(@RequestBody AspUser body)
    {
        AspUser aspUser=new AspUser();
        aspUser.setCode(-1);
        aspUser.setNote("");
        try{


            System.out.println(body.toString());
            SqlSession sess=factory.openSession();
            AspUserDao aspUserDao=sess.getMapper(AspUserDao.class);
            aspUser=aspUserDao.delete(body);
            sess.close();
            System.out.println(aspUser);
            System.out.println("销户结束");
            return aspUser;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspUser;
        }
    }

    /**
     * 根据uId获得用户信息
     * http://localhost:9090/user/get
     * @param body
     * @return
     */
    @PostMapping("getAll")
    @ResponseBody
    public List<AspUser> getAll(@RequestBody AspUser body)
    {
        List<AspUser> aspUser=null;
        System.out.println(body);
        try{
            System.out.println(body.toString());
            SqlSession sess=factory.openSession();
            AspUserDao aspUserDao=sess.getMapper(AspUserDao.class);
            aspUser=aspUserDao.getAll(body);
            sess.close();
            System.out.println(aspUser);
            System.out.println("根据获得用户信息结束");
            return aspUser;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspUser;
        }
    }

    /**
     * 根据uId获得用户信息
     * http://localhost:9090/user/get
     * @param body
     * @return
     */
    @PostMapping("get")
    @ResponseBody
    public AspUser get(@RequestBody AspUser body)
    {
        AspUser aspUser=new AspUser();
        aspUser.setCode(-1);
        aspUser.setNote("");
        System.out.println(body);
        try{
            System.out.println(body.toString());
            SqlSession sess=factory.openSession();
            AspUserDao aspUserDao=sess.getMapper(AspUserDao.class);
            aspUser=aspUserDao.selectByPrimaryKey(body.getuId());
            sess.close();
            aspUser.setCode(1);
            aspUser.setNote("查询用户成功");
            System.out.println(aspUser);
            System.out.println("根据主键获得用户信息结束");
            return aspUser;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspUser;
        }
    }

    /**
     * 修改密码
     *http://localhost:9090/user/updatePwd
     * @param body
     * @return
     */
    @PostMapping("updatePwd")
    @ResponseBody
    public AspUser updatePwd(@RequestBody UpdatePwd body)
    {
        AspUser aspUser=new AspUser();
        aspUser.setCode(-1);
        aspUser.setNote("");
        System.out.println(body);
        try{
            System.out.println(body.toString());
            SqlSession sess=factory.openSession();
            AspUserDao aspUserDao=sess.getMapper(AspUserDao.class);
            aspUser=aspUserDao.updatePwd(body);
            sess.close();
            System.out.println(aspUser);
            System.out.println("修改结束");
            return aspUser;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspUser;
        }
    }

    /**
     * 用户个人信息修改
     * http://localhost:9090/user/update
     * @param body
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public AspUser update(@RequestBody AspUser body)
    {
        AspUser aspUser=new AspUser();
        aspUser.setCode(-1);
        aspUser.setNote("");
        try{
            System.out.println(body.toString());
            SqlSession sess=factory.openSession();
            AspUserDao aspUserDao=sess.getMapper(AspUserDao.class);
            aspUser=aspUserDao.update(body);
            sess.close();
            System.out.println(aspUser);
            System.out.println("修改成功");
            return aspUser;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspUser;
        }
    }

    /**
     * 用户注册
     * http://localhost:9090/user/register
     * @param body
     * @return
     */
    @PostMapping("register")
    @ResponseBody
    public AspUser register(@RequestBody AspUser body)
    {
        AspUser aspUser=new AspUser();
        aspUser.setCode(-1);
        aspUser.setNote("");
        try{
            //获得当前日期（用户创建日期）
            long time=System.currentTimeMillis();
            Date date=new Date(time);
            String mat="yyyyMMdd";
            SimpleDateFormat format=new SimpleDateFormat(mat);
            String strDate=format.format(date);
            int createDate=Integer.parseInt(strDate);
            body.setuCreatedate(createDate);


            System.out.println(body.toString());
            SqlSession sess=factory.openSession();
            AspUserDao aspUserDao=sess.getMapper(AspUserDao.class);
            aspUser=aspUserDao.register(body);
            sess.close();
            System.out.println(aspUser);
            System.out.println("注册成功");
            return aspUser;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspUser;
        }
    }

    /**
     * 登录验证
     * http://localhost:9090/user/login
     * @param body
     * @return
     * /*@throws AspException
     */
    @PostMapping("login")
    @ResponseBody
    public AspUser login(@RequestBody AspUser body) //throws AspException
    {
        AspUser aspUser=new AspUser();
        aspUser.setCode(-1);
        aspUser.setNote("");
        try{
            System.out.println(body.toString());
//            int o_code;
//            String o_note;
//            Map<String,Object> map =new HashMap<String,Object>();
//            map.put("i_login",body.getuLogin());
//            map.put("i_pwd",body.getuPwd());
            SqlSession sess=factory.openSession();
            AspUserDao aspUserDao=sess.getMapper(AspUserDao.class);
            aspUser=aspUserDao.login(body);
//        o_code=(int)map.get("o_code");
//        o_note=(String)map.get("o_onte");
            sess.close();
//        if(o_code<0){
//            throw new AspException(o_code,o_note);
//        }
            System.out.println(aspUser);
            System.out.println("登陆验证成功");
            return aspUser;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspUser;
        }

    }
}
